package com.jcompressor.faces.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.context.FacesContext;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrBuilder;

import com.jcompressor.annotations.JcompressorResource;
import com.jcompressor.annotations.JcompressorResources;
import com.jcompressor.config.Configurator;
import com.jcompressor.faces.annotations.FacesAnnotationScanner;
import com.jcompressor.model.config.Resource;

public class FacesConfigurator extends Configurator {
	
	public FacesConfigurator() {
		super();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	protected void scanAnnotations() {
		final FacesAnnotationScanner scanner = new FacesAnnotationScanner(new Class[] {JcompressorResources.class, JcompressorResource.class, ResourceDependencies.class, ResourceDependency.class});
		scanner.scan();
		
		final List<Resource> resources = new ArrayList<Resource>();
		final FacesContext facesContext = FacesContext.getCurrentInstance();
		for (final Resource resource : scanner.getResources()) {
			this.getResourcesConfiguration().addResource(resource);
			
			if (StringUtils.isNotBlank(resource.getLibrary())) {
				final javax.faces.application.Resource jsfResource = facesContext.getApplication().getResourceHandler().createResource(resource.getName(), resource.getLibrary());
				if (jsfResource != null) {
					final StrBuilder path = new StrBuilder();
					if (!StringUtils.startsWith(resource.getLibrary(), "/")) {
						path.append('/');
					}
					
					path.append(resource.getLibrary());
					path.append('/');
					path.append(resource.getName());
					
					if (StringUtils.isBlank(resource.getId())) {
						try {
							final StrBuilder builder = new StrBuilder();
							for (final String line : IOUtils.readLines(facesContext.getExternalContext().getResourceAsStream(path.toString()), "UTF-8")) {
								builder.append(line);
							}
							
							resource.setContent(builder.toString());
						}
						catch (final IOException e) {
							e.printStackTrace();
						}
					}
					else {
						try {
							final StrBuilder builder = new StrBuilder();
							for (final String line : IOUtils.readLines(facesContext.getExternalContext().getResourceAsStream("/WEB-INF/classes/META-INF/resources" + path.toString()), "UTF-8")) {
								builder.append(line);
							}
							
							resource.setContent(builder.toString());
						}
						catch (final IOException e) {
							e.printStackTrace();
						}
					}
				}
				
				resources.add(resource);
			}
		}
		
		this.setResources(resources);
	}
	
	@Override
	public void loadResources() {
		//try {
			/*
			final FacesContext facesContext = FacesContext.getCurrentInstance();
			for (final Resource resource : this.getResourcesConfiguration().getResources()) {
				final javax.faces.application.Resource jsfResource = facesContext.getApplication().getResourceHandler().createResource(resource.getTarget(), resource.getLibrary());
				if (jsfResource != null) {
					resource.setInputStream(jsfResource.getInputStream());
				}
			}
			*/
			
			/*
			for (final Map.Entry<String, Resource> entry : this.getConfiguration().getResources().entrySet()) {
				final javax.faces.application.Resource jsfResource = facesContext.getApplication().getResourceHandler().createResource(entry.getValue().getTarget(), entry.getValue().getLibrary());
				if (jsfResource != null) {
					entry.getValue().setInputStream(jsfResource.getInputStream());
				}
			}
			*/
		//}
		//catch (final IOException e) {
		//	throw new JcompressorException(e);
		//}
	}
	
}